package applet.attendee.unlocked.components.events;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;
import data.account.attendee.AttendeeAccount;
import data.event.Event;
import data.event.EventTable;

import applet.attendee.CMSAttendeeView;
import applet.misc.action.EnableActionListener;
import applet.misc.graphics.AttendeeBackgroundPanel;

public class CMSAttendeeEventsView extends AttendeeBackgroundPanel
{
	private static final long serialVersionUID = 6490614358660371039L;
	
	private CMSAttendeeView attendeeView;
	public CMSAttendeeView attendeeView() { return attendeeView; }
	
	private EventTable unattendedEvents;
	private EventTable attendedEvents;
	
	private JButton saveButton;

	public CMSAttendeeEventsView(CMSAttendeeView attendeeView)
	{
		super(new GridBagLayout());
		this.attendeeView = attendeeView;
		setBorder(new EmptyBorder(25, 25, 25, 25));
		defineComponents();
		
		JPanel eventsDialog = new JPanel(new BorderLayout());
		eventsDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		eventsDialog.setPreferredSize(new Dimension(600, 500));
		
			JPanel titlePane = new JPanel(new BorderLayout());
			titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
			titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel title = new JLabel("Events");
				title.setFont(title.getFont().deriveFont(Font.BOLD));
				titlePane.add(title, BorderLayout.WEST);
				eventsDialog.add(titlePane, BorderLayout.NORTH);
			
			JPanel split = new JPanel(new GridLayout(1, 2));
			split.setBorder(new BevelBorder(BevelBorder.LOWERED));
				split.add(getUnattendedEventsList());
				split.add(getAttendedEventsList());
				
		eventsDialog.add(split, BorderLayout.CENTER);
		eventsDialog.add(getSavePanel(), BorderLayout.SOUTH);
		
		add(eventsDialog);
	}
	
	private void defineComponents()
	{
		saveButton = new JButton("Save Events");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.setEnabled(false);
		saveButton.addActionListener(new EventSaveListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));

		ArrayList<Event> unattended = new ArrayList<Event>();
		ArrayList<Event> attended = new ArrayList<Event>();
		
		for(Event event : CMSRes.conference().events())
			if(event.attendees().contains(attendeeView().attendee().userID()))
				attended.add(event);
			else
				unattended.add(event);
		
		unattendedEvents = new EventTable(unattended);
		attendedEvents = new EventTable(attended);
	}
	
	private JPanel getSavePanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(saveButton, BorderLayout.EAST);
		
		return panel;
	}
	
	private JPanel getUnattendedEventsList()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED));

			JPanel layoutHelper = new JPanel(new GridBagLayout());
			layoutHelper.setBackground(CMSRes.Colors.get("Theme Color Light"));
			layoutHelper.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED),
					new EmptyBorder(10, 10, 10, 10)));
				JLabel title = new JLabel("Unattended events");
				title.setFont(title.getFont().deriveFont(16f));
				title.setFont(title.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(title);
		
		panel.add(layoutHelper, BorderLayout.NORTH);
		panel.add(new JScrollPane(unattendedEvents), BorderLayout.CENTER);
		
			JPanel controlPane = new JPanel(new GridLayout(1, 0));
		
				JButton addEventButton = new JButton("Add Event");
				addEventButton.setPreferredSize(new Dimension(200, 30));
					addEventButton.addActionListener(new EventTransferListener(
							unattendedEvents, attendedEvents, true));
				controlPane.add(addEventButton);
			
				JButton viewEventButton = new JButton("View Event");
				viewEventButton.setPreferredSize(new Dimension(200, 30));
				viewEventButton.addActionListener(new EventViewListener(unattendedEvents));
				controlPane.add(viewEventButton);
				
				panel.add(controlPane, BorderLayout.SOUTH);
		return panel;
	}
	
	private JPanel getAttendedEventsList()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		
			JPanel layoutHelper = new JPanel(new GridBagLayout());
			layoutHelper.setBackground(CMSRes.Colors.get("Theme Color Light"));
			layoutHelper.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED),
					new EmptyBorder(10, 10, 10, 10)));
				JLabel title = new JLabel("Attended events");
				title.setFont(title.getFont().deriveFont(16f));
				title.setFont(title.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(title);
		
		panel.add(layoutHelper, BorderLayout.NORTH);
		panel.add(new JScrollPane(attendedEvents), BorderLayout.CENTER);
		
			JPanel controlPane = new JPanel(new GridLayout(1, 0));
				
				JButton removeEventButton = new JButton("Remove Event");
				removeEventButton.setPreferredSize(new Dimension(200, 30));
					removeEventButton.addActionListener(new EventTransferListener(
							attendedEvents, unattendedEvents, false));
				controlPane.add(removeEventButton);
				
				JButton viewEventButton = new JButton("View Event");
				viewEventButton.setPreferredSize(new Dimension(200, 30));
				viewEventButton.addActionListener(new EventViewListener(attendedEvents));
				controlPane.add(viewEventButton);
				
			panel.add(controlPane, BorderLayout.SOUTH);
		return panel;
	}
	
	private class EventTransferListener implements ActionListener
	{
		private EventTable sourceEvents;
		private EventTable targetEvents;
		private boolean checkConflicts;
		
		public EventTransferListener(EventTable sourceEvents, 
				EventTable targetEvents, boolean checkConflicts)
		{
			this.sourceEvents = sourceEvents;
			this.targetEvents = targetEvents;
			this.checkConflicts = checkConflicts;
		}

		@Override public void actionPerformed(ActionEvent a)
		{
			if(checkConflicts)
				if(sourceEvents.canAddSelectedTo(targetEvents))
				{
					saveButton.setEnabled(true);
					sourceEvents.transferSelectedTo(targetEvents);
				}
				else
				{
					JOptionPane.showMessageDialog(CMSRes.frame(), 
							"You are attending an event that conflicts with the one selected!",
							"Event Conflict",
							JOptionPane.WARNING_MESSAGE);
				}
			else
			{
				saveButton.setEnabled(true);
				sourceEvents.transferSelectedTo(targetEvents);
			}
		}
	}
	
	private class EventSaveListener implements ActionListener
	{	
		@Override public void actionPerformed(ActionEvent e)
		{
			AttendeeAccount attendee = attendeeView().attendee();
			for(Event event : CMSRes.conference().events())
				if(attendedEvents.events().contains(event))
				{
					if(!event.attendees().contains(attendee.userID()))
						event.attendees().add(attendee.userID());
				}
				else
					event.attendees().remove(attendee.userID());
			CMSRes.conference().saveData();
		}
	}
	
	private class EventViewListener implements ActionListener
	{
		private EventTable events;
		
		public EventViewListener(EventTable events) { this.events = events; }

		@Override public void actionPerformed(ActionEvent e)
		{
			Event event = events.getSelected();
			if(event != null)
			{
				EventDialog dialog = new EventDialog(event);
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
			}
		}
	}
}